package com.lframework.xingyun.promo.impl.nthpiece;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lframework.starter.web.core.impl.BaseMpServiceImpl;
import com.lframework.starter.web.core.utils.PageResultUtil;
import com.lframework.starter.web.core.components.resp.PageResult;
import com.lframework.starter.web.core.utils.OpLogUtil;
import java.io.Serializable;
import com.lframework.starter.common.exceptions.impl.DefaultClientException;
import com.lframework.starter.common.utils.ObjectUtil;
import com.lframework.starter.web.core.annotations.oplog.OpLog;
import com.lframework.starter.common.utils.Assert;
import com.lframework.starter.web.inner.components.oplog.OtherOpLogType;
import com.lframework.xingyun.promo.entity.MarketingNthpiece;
import com.lframework.xingyun.promo.mappers.MarketingNthpieceMapper;
import com.lframework.xingyun.promo.service.nthpiece.MarketingNthpieceService;
import com.lframework.xingyun.promo.vo.nthpiece.CreateMarketingNthpieceVo;
import com.lframework.xingyun.promo.vo.nthpiece.QueryMarketingNthpieceVo;
import com.lframework.xingyun.promo.vo.nthpiece.UpdateMarketingNthpieceVo;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class MarketingNthpieceServiceImpl extends BaseMpServiceImpl<MarketingNthpieceMapper, MarketingNthpiece> implements MarketingNthpieceService {

    @Override
    public PageResult<MarketingNthpiece> queryPage(Integer pageIndex, Integer pageSize, QueryMarketingNthpieceVo vo) {

        Assert.greaterThanZero(pageIndex);
        Assert.greaterThanZero(pageSize);

        IPage<MarketingNthpiece> page = getBaseMapper().queryPage(vo);

        return PageResultUtil.convert(page);
    }

    @Override
    public List<MarketingNthpiece> queryList(QueryMarketingNthpieceVo vo) {

        return getBaseMapper().queryList(vo);
    }

    @Cacheable(value = MarketingNthpiece.CACHE_NAME, key = "@cacheVariables.tenantId() + #id", unless = "#result == null")
    @Override
    public MarketingNthpiece findById(String id) {

        return getBaseMapper().selectById(id);
    }

    @OpLog(type = OtherOpLogType.class, name = "新增限时第N件活动表，ID：{}", params = {"#id"})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public String create(CreateMarketingNthpieceVo vo) {

        MarketingNthpiece data = new MarketingNthpiece();
        data.setName(vo.getName());
        data.setBeginDate(vo.getBeginDate());
        data.setEndDate(vo.getEndDate());
        data.setCycleFlag(vo.getCycleFlag());
        data.setCycleType(vo.getCycleType());
        data.setCycleTimeGroup(vo.getCycleTimeGroup());
        data.setCycleTimeRule(vo.getCycleTimeRule());
        data.setAllMemberLevel(vo.getAllMemberLevel());
        data.setMemberLevelId(vo.getMemberLevelId());
        data.setAllBuyLimit(vo.getAllBuyLimit());
        data.setCycleBuyLimit(vo.getCycleBuyLimit());
        data.setAllProduct(vo.getAllProduct());
        data.setProductId(vo.getProductId());
        data.setUseCoupon(vo.getUseCoupon());
        data.setAllShop(vo.getAllShop());
        data.setShopId(vo.getShopId());
        data.setPromFlag(vo.getPromFlag());
        data.setDiscountType(vo.getDiscountType());
        data.setPromItem(vo.getPromItem());

        getBaseMapper().insert(data);

        OpLogUtil.setVariable("id", data.getId());
        OpLogUtil.setExtra(vo);

        return data.getId();
    }

    @OpLog(type = OtherOpLogType.class, name = "修改限时第N件活动表，ID：{}", params = {"#id"})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(UpdateMarketingNthpieceVo vo) {

        MarketingNthpiece data = getBaseMapper().selectById(vo.getId());
        if (ObjectUtil.isNull(data)) {
            throw new DefaultClientException("限时第N件活动表不存在！");
        }

        LambdaUpdateWrapper<MarketingNthpiece> updateWrapper = Wrappers.lambdaUpdate(MarketingNthpiece.class)
                .set(MarketingNthpiece::getName, vo.getName())
                .set(MarketingNthpiece::getBeginDate, vo.getBeginDate())
                .set(MarketingNthpiece::getEndDate, vo.getEndDate())
                .set(MarketingNthpiece::getCycleFlag, vo.getCycleFlag())
                .set(MarketingNthpiece::getCycleType, vo.getCycleType())
                .set(MarketingNthpiece::getCycleTimeGroup, vo.getCycleTimeGroup())
                .set(MarketingNthpiece::getCycleTimeRule, vo.getCycleTimeRule())
                .set(MarketingNthpiece::getAllMemberLevel, vo.getAllMemberLevel())
                .set(MarketingNthpiece::getMemberLevelId, vo.getMemberLevelId())
                .set(MarketingNthpiece::getAllBuyLimit, vo.getAllBuyLimit())
                .set(MarketingNthpiece::getCycleBuyLimit, vo.getCycleBuyLimit())
                .set(MarketingNthpiece::getAllProduct, vo.getAllProduct())
                .set(MarketingNthpiece::getProductId, vo.getProductId())
                .set(MarketingNthpiece::getUseCoupon, vo.getUseCoupon())
                .set(MarketingNthpiece::getAllShop, vo.getAllShop())
                .set(MarketingNthpiece::getShopId, vo.getShopId())
                .set(MarketingNthpiece::getPromFlag, vo.getPromFlag())
                .set(MarketingNthpiece::getDiscountType, vo.getDiscountType())
                .set(MarketingNthpiece::getPromItem, vo.getPromItem())
                .eq(MarketingNthpiece::getId, vo.getId());

        getBaseMapper().update(updateWrapper);

        OpLogUtil.setVariable("id", data.getId());
        OpLogUtil.setExtra(vo);
    }

    @CacheEvict(value = MarketingNthpiece.CACHE_NAME, key = "@cacheVariables.tenantId() + #key")
    @Override
    public void cleanCacheByKey(Serializable key) {

    }
}

